package ar.edu.unq.wannaplay.pages;

import java.util.ArrayList;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PageableListView;
import org.apache.wicket.spring.injection.annot.SpringBean;

import ar.edu.unq.wannaplay.components.WPLink;
import ar.edu.unq.wannaplay.dtos.CareerPlanOrganizerDTO;
import ar.edu.unq.wannaplay.pages.abm.bands.AddBandPage;
import ar.edu.unq.wannaplay.pages.abm.bands.EditBandPage;
import ar.edu.unq.wannaplay.services.CareerPlanService;

public class BandPage extends WebPage {
    private static final long serialVersionUID = 1L;

    @SpringBean(name = "service.careerPlanService")
    private CareerPlanService careerPlanService;

    private CareerPlanOrganizerDTO careerPlan;

    public BandPage() {
    }

    protected void init() {
    	careerPlan = this.getCareerPlanService().getCareerPlanOrganizer();
        this.add(new PageableListView<String>("tablaBandas", new ArrayList<String>(this.getCareerPlan().getRanks()
                .keySet()), 5) {
            private static final long serialVersionUID = 1L;
            PageParameters parameter;

            @Override
            protected void populateItem(final ListItem<String> item) {
                final String currentRank = item.getModelObject();
                item.add(new Label("rankName", currentRank));
                String currentPercentage = BandPage.this.getCareerPlan().getRanks().get(currentRank).toString();
                item.add(new Label("technicians", currentPercentage));
                item.add(new Label("testers", currentPercentage));
                item.add(new Label("functionals", currentPercentage));
                item.add(new Label("designers", currentPercentage));

                parameter = new PageParameters();
                parameter.add("rank", currentRank);
                item.add(new WPLink<EditBandPage>("editBand", EditBandPage.class, parameter));
                item.add(new Link<WebPage>("deleteBand") {
                    private static final long serialVersionUID = 1L;

                    @Override
                    public void onClick() {
                        BandPage.this.getCareerPlan().setDeletedRank(currentRank);
                        BandPage.this.getCareerPlanService().updateDeletedRank(BandPage.this.getCareerPlan());
                        BandPage.this.init();
//                        this.setResponsePage(BandPage.class);
                    }
                });
            }
        });

        this.add(new WPLink<AddBandPage>("addBand", AddBandPage.class));
    }

    // setters&getters
    public void setCareerPlanService(final CareerPlanService careerPlanService) {
        this.careerPlanService = careerPlanService;
    }

    public CareerPlanService getCareerPlanService() {
        return careerPlanService;
    }

    public void setCareerPlan(final CareerPlanOrganizerDTO careerPlan) {
        this.careerPlan = careerPlan;
    }

    public CareerPlanOrganizerDTO getCareerPlan() {
        return careerPlan;
    }
}
